package com.examlpe.cels;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Map.Entry;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import com.examlpe.cels.entity.Vocabulary;
import com.examlpe.cels.entity.Word;
import com.examlpe.cels.exception.FileNotFoundException;
import com.examlpe.cels.mapper.VocabularyMapper;
import com.examlpe.cels.mapper.WordMapper;
import com.examlpe.cels.service.IBrowserService;

public class testq1 {

	private static SqlSessionFactory ssf;

	@Before
	public void setUp() throws IOException {

		String resource = "MybatisConfig.xml";
		InputStream input = Resources.getResourceAsStream(resource);

		ssf = new SqlSessionFactoryBuilder().build(input);

	}

//	 @Test
//	 public void testAllWord() {
//	 SqlSession ss = ssf.openSession();
//	
//	 //Mapper接口对象
//	 WordMapper wm = ss.getMapper(WordMapper.class);
//	
//	 Set<Word> words = wm.findAllWord();
//	 for(Word word:words) {
//	 System.out.println(word);
//	 }
//	 ss.close();
//	 }

	// @Test
	// public void testAllVocabulary() {
	// SqlSession ss = ssf.openSession();
	//
	// //Mapper接口对象
	// VocabularyMapper vm = ss.getMapper(VocabularyMapper.class);
	// Set<Vocabulary> vocabularys = vm.findAllVocabulary();
	// for(Vocabulary vocabulary:vocabularys) {
	// System.out.println(vocabulary);
	// }
	// ss.close();
	// }

//	@Test
//	public void testAllVocabulary() {
//		SqlSession ss = ssf.openSession();
//
//		// Mapper接口对象
//		VocabularyMapper vm = ss.getMapper(VocabularyMapper.class);
//		Set<Vocabulary> vocabularys = vm.findAllVocabulary();
//		for (Vocabulary vocabulary : vocabularys) {
//			System.out.println(vocabulary);
//		}
//		ss.close();
//	}

	public void showWord() {
//		testq1 ib=null;
		Map<Character,Set<Word>> map = testq1.getFirstMap();
		Set<Entry<Character,Set<Word>>> set = map.entrySet();
		Iterator<Entry<Character,Set<Word>>> it = set.iterator();
		int count=0;
		while(it.hasNext()) {
			Entry<Character,Set<Word>> entry = it.next();
			char key = entry.getKey();
			Set<Word> value = entry.getValue();
			System.out.print(key+"["+value.size()+"]\t");
			count++;
			if(count%5==0) {
				System.out.println();
			}
		}
		System.out.println("请选择>");
		//showAllWords(map);
	}
	
	@Test
	public  void main() {
		testq1 ibs = new testq1();
		ibs.showWord();
	}
	public static Map<Character, Set<Word>> getFirstMap() {
		Map<Character, Set<Word>> datas = new TreeMap<Character, Set<Word>>();

		SqlSession ss = ssf.openSession();
		WordMapper wm = ss.getMapper(WordMapper.class);
		Set<Word> words = wm.findAllWord();
		
		Set<Word> wSet = new TreeSet<Word>(words);
		Set<Word> set = null;
		for (Word w : wSet) {
			if (datas.containsKey(w.getFirst())) {
				set = datas.get(w.getFirst());
				set.add(w);
				datas.put(w.getFirst(), set);
			} else {
				set = new TreeSet<Word>();
				set.add(w);
				datas.put(w.getFirst(), set);
			}
		}
		return datas;
	}
}
